<?php

class Dp3 extends Controller {

	function Dp3() {
		parent::Controller();
        $this->load->model('W_dp3_mod', 'p');
        $this->load->model('Datum_model'); 
	}
	
	function index() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}

		$data['additional_js'] = '
		<script type="text/javascript">
		function deleteRow(id) {
			var txt = \'Anda yakin mau menghapus data DP-3 ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
				url = "'.base_url().'index.php/dp3/hapus/"+id;
			 
					$.prompt(txt,{
						buttons:{Hapus:true, Batal:false},
						callback: function(v){
							if(v) {
								$("tr#row-"+id).fadeOut("slow", function() {
									$("tr#row-"+id).remove();
								});
								top.location.href=url;
							}
						}
					});
				 }	
		</script>
         <script type="text/javascript">
            $(function() {
              
                $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/hukuman\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules().'
                });
                //autocomplete
   
                $("#nip").autocomplete("'.base_url().'index.php/autocomplit/nip", {
                    width: 200,
                    selectFirst: true
                });
                
                
                $("#nip").result(function(event, data, formatted) {
                    if (data)
                        $("#id_pegawai").val(data[1]);
                });
            
            });
        </script>
		';
		if( $data['role'] == 0)
            $nip = $this->session->userdata('nip');
        else if ($this->uri->segment(3) == 'nip')
            $nip = $this->uri->segment(4);
        else
		    $nip = $this->input->post('nip')?$this->input->post('nip'):"";  
        $this->session->set_userdata(array('nip' => $nip));    
        $data['judul'] = 'RIWAYAT DATA DP-3';
        $data['list_data'] = $this->p->list_dp3($nip);
        $data['tampilkan_menu'] = true;
        $data['panel'] = $this->_setPanel($nip);
        
        if($this->input->post('page'))echo $data['list_data'];
        else $this->load->view('view_dp3',$data);
    }

    function _setPanel($nip) {
        $info_pegawai = '';
        if ($nip!="") {
            $this->db->select('gelar_depan, nama, gelar_belakang');
            $this->db->where(array('nip' => $nip));
            $this->db->or_where(array('nip_baru' => $nip));
            $query = $this->db->get('pegawai');
            if ($query->num_rows() > 0) {
                $r = $query->row();
                $info_pegawai = sprintf("%s %s %s", $r->gelar_depan, $r->nama, $r->gelar_belakang);
            } else {
                $info_pegawai = '-';
            }
        }
        $ret =  form_open('dp3').
                "<fieldset >
                 <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
                  <tr>
                    <td class=\"kolom\" valign=\"top\">NIP</td>
                    <td valign=\"top\">:</td>
                    <td valign=\"top\"><input type=\"text\" name=\"nip\" id=\"nip\" value=\"$nip\" />
                    <input name='id_pegawai' type='hidden' id='id_pegawai' /> 
                    </td>
                    <td valign=\"top\"><input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"Tampilkan\" /></td>
                    <td valign=\"top\"><div id=\"info_pegawai\">".$info_pegawai."</div></td>
                 </tr>
                 </table>
                 </fieldset>".
                 form_close();
        return $ret;        
    }
    
    function _setRules() {
    	return '
                    //set the rules for the field names
                    rules: {
                        //id_pegawai: { required: true },
                        nip: { required: true },
                      
                        
                    },
                    //set messages to appear inline
                    messages: {
                        //id_pegawai: "NIP tidak terdaftar",
                        nip: "NIP wajib diisi /",
                        
                               
    				}
    	';
    }
    
    function form_isian() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $form_action = base_url().'index.php/dp3/simpan';
        $form_id = 'form_dp3';
        
    	
        $data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		      
                $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/dp3\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules().'
                });
                //autocomplete
   
                
            
        	});
        </script>
		';

           
        $data['judul'] = 'INPUT DP-3 (2 tahun terakhir) ';
        $data['list_data'] = $this->p->entry_dp3($form_id,$form_action);
        $this->load->view('view_dp3', $data);
    }
    
    function simpan($is_update=FALSE,$nip='') {
       if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        if ($this->input->post('id_pegawai')) {          
            $id_pegawai = $this->d->getData('idspg_pegawai', 'pegawai', array('nip_baru' => $this->input->post('nip')));
            $arr['id_pegawai'] = $id_pegawai!=false?$id_pegawai:$this->d->getData('idspg_pegawai', 'pegawai', array('nip' => $this->input->post('nip')));
        } 
        
        if ($this->input->post('tahun')) {          
        	$arr['tahun'] = $this->input->post('tahun');
        } else {
        	$arr['tahun'] = "";
        }
        if ($this->input->post('pejabat_penilai')) {
        	$arr['pejabat_penilai'] = $this->input->post('pejabat_penilai');
        } else {
        	$arr['pejabat_penilai'] = "";
        }
        if ($this->input->post('atasan_pejabat_penilai')) {
            $arr['atasan_pejabat_penilai'] = $this->input->post('atasan_pejabat_penilai');
        } else {       
            $arr['atasan_pejabat_penilai'] = "";
        }
        if ($this->input->post('nilai')) {
            $arr['nilai'] = $this->input->post('nilai');
        } else {       
            $arr['nilai'] = "";
        }        
       
        
        
        if ($is_update) {
        	$this->p->simpan($arr, $is_update, $id);
        } else {
        	$this->p->simpan($arr);
        }

        redirect('dp3/index/nip/'.$nip);
    }
    
    function form_edit($id) {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
    	$form_action = base_url().'index.php/dp3/simpan/true/'.$id;
        $form_id = 'form_dp3';  
    	
        $data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		        
                $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/dp3\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules().'
                });
                
                
                
        	});
        </script>
		';
        
        $this->db->where(array('idspg_dp3' => $id));
        $q = $this->db->get('dp3');
        if ($q->num_rows() > 0) {
            $row = $q->row();
            
            $nama = Datum_model::getData('nama','pegawai','idspg_pegawai ='.$row->id_pegawai.'');
            $nip2 = Datum_model::getData('nip_baru','pegawai','idspg_pegawai ='.$row->id_pegawai.'');
            $nip = $nip2!=''?$nip2:Datum_model::getData('nip','pegawai','idspg_pegawai ='.$row->id_pegawai.'');
                
            $arr['id']          = $row->id_pegawai;
            $arr['nip']         = $nip;
            $arr['tahun']     = $row->tahun;
            $arr['pejabat_penilai']       = $row->pejabat_penilai;
            $arr['atasan_pejabat_penilai']     = $row->atasan_pejabat_penilai;
            $arr['nilai']     = $row->nilai;
            
      
            $form_action = base_url().'index.php/dp3/simpan/true/'.$id.'/'.$nip;
            $form_id = 'form_dp3';
            
            $data['judul'] = 'UBAH DATA DP-3 : '.$nama;
            $data['list_data'] = $this->p->entry_dp3($form_id,$form_action,$arr);
            $this->load->view('view_dp3', $data);
        } else {
            redirect('dp3');
        }
    }


    function hapus($id) {
        $this->db->where('idspg_dp3', $id);
        $this->db->delete('dp3');
        redirect('dp3');
    }
    

    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */